package com.lzc.tlmail.tlmailcoupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lzc.tlmail.tlmailcoupon.entity.SeckillSkuRelationEntity;
import com.lzc.tlmail.tlmailcoupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzc.common.utils.PageUtils;
import com.lzc.common.utils.Query;

import com.lzc.tlmail.tlmailcoupon.dao.SeckillSessionDao;
import com.lzc.tlmail.tlmailcoupon.entity.SeckillSessionEntity;
import com.lzc.tlmail.tlmailcoupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询最近三天参与秒杀的商品
     * @return
     */
    @Override
    public List<SeckillSessionEntity> uploadSeckillSkuLatest3Days() {
        SeckillSessionDao seckillSessionDao = this.getBaseMapper();
       String startTime=this.getStartTime();
        String endTime=this.getEndTime();
        List<SeckillSessionEntity> seckillSessionEntities=seckillSessionDao.uploadSeckillSkuLatest3Days(startTime,endTime);
        if(seckillSessionEntities!=null&&seckillSessionEntities.size()>0){
            List<SeckillSessionEntity> sessionEntities = seckillSessionEntities.stream().map(item -> {
                Long id = item.getId();
                List<SeckillSkuRelationEntity> relationEntities = seckillSkuRelationService.list(new LambdaQueryWrapper<SeckillSkuRelationEntity>().eq(SeckillSkuRelationEntity::getPromotionSessionId, id));
                item.setSkuRelationEntityList(relationEntities);
                return item;
            }).collect(Collectors.toList());
            return sessionEntities;
        }
        return null;
    }

    /**
     * 获取结束时间 结束时间就是当前时间加三天 这样就能得到最近三天了
     * @return
     */
    private String getEndTime() {
        LocalDate now = LocalDate.now();
        LocalTime MAX = LocalTime.MAX;
        LocalDate localDate = now.plusDays(2);
        LocalDateTime of = LocalDateTime.of(localDate, MAX);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String endTime = dateTimeFormatter.format(of);
        return endTime;
    }

    /**
     * 获取当前时间
     * @return
     */
    private String getStartTime() {
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime of = LocalDateTime.of(now, min);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
       return dateTimeFormatter.format(of);
    }


}